package com.kh.chart;

import com.kh.domain.ChannelData;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CombinedDomainXYPlot;
import org.jfree.chart.plot.ValueMarker;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.general.Dataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ChartHelper {

    public static ChartPanel createChartPanel(CombinedDomainXYPlot combinedDomainXYPlot) {
        JFreeChart freeChart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, combinedDomainXYPlot, false);
        ChartPanel chartPanel = new WaveChartPanel(freeChart);
        chartPanel.setRangeZoomable(false);
        chartPanel.setDomainZoomable(false);
        chartPanel.setMouseZoomable(false);
        chartPanel.setMouseWheelEnabled(false);
        System.out.println(chartPanel.isDoubleBuffered());
        chartPanel.setDoubleBuffered(true);
        chartPanel.setRefreshBuffer(true);
        final XYPlot plot = (XYPlot) freeChart.getPlot();
        final ValueAxis domainAxis = plot.getDomainAxis();
//        domainAxis.setTickLabelsVisible(false);
        final ValueAxis rangeAxis = plot.getRangeAxis();
        return chartPanel;
    }

    public static ValueMarker createSolidMarker(double value) {
        final ValueMarker valueMarker = new ValueMarker(value);
        float width = 3.0f;
        valueMarker.setStroke(new BasicStroke(width));
        valueMarker.setPaint(Color.red);
        return valueMarker;
    }

    public static ValueMarker createDashMarker(double value) {
        final ValueMarker valueMarker = new ValueMarker(value);
        float width = 3.0f;
        final BasicStroke basicStroke = new BasicStroke(width, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL,
                5.0f, new float[] {15f}, 10.0f);;
        valueMarker.setStroke(basicStroke);
        valueMarker.setPaint(Color.BLUE);
        return valueMarker;
    }
    /**
     * 所有通道转换为一个数据集
     * @param channelDataList
     * @return
     */
    public static List<XYDataset> transFromMode1(List<ChannelData> channelDataList) {
        List<XYDataset> datasetList = new ArrayList<>();
        XYSeriesCollection collection = new XYSeriesCollection();
        for (ChannelData channelData : channelDataList) {
            XYSeries series = new XYSeries(channelData.getTotalDesc());
            final List<Double> values = channelData.getValues();
            for (int i = 0; i < values.size(); i++) {
                series.add(i, values.get(i));
            }
            collection.addSeries(series);
        }
        datasetList.add(collection);
        return datasetList;
    }

    /**
     * 一个设备转换为一个数据集
     * @param channelDataList
     * @return
     */
    public static List<XYDataset> transFromMode2(List<ChannelData> channelDataList) {
        List<XYDataset> datasetList = new ArrayList<>();
        Map<String, List<ChannelData>> deviceDataMap = new HashMap<>();
        channelDataList.stream().forEach(channelData -> {
            final String deviceName = channelData.getDeviceName();
            if (!deviceDataMap.containsKey(deviceName)) {
                deviceDataMap.put(deviceName, new ArrayList<>());
            }
            deviceDataMap.get(deviceName).add(channelData);
        });

        deviceDataMap.forEach((deviceName, dataList) -> {
            XYSeriesCollection collection = new XYSeriesCollection();
            for (ChannelData channelData : dataList) {
                XYSeries series = new XYSeries(channelData.getTotalDesc());
                final List<Double> values = channelData.getValues();
                for (int i = 0; i < values.size(); i++) {
                    series.add(i, values.get(i));
                }
                collection.addSeries(series);
            }
            datasetList.add(collection);
        });

        return datasetList;
    }

    /**
     * 一个通道转换为一个数据集
     * @param channelDataList
     * @return
     */
    public static List<XYDataset> transFromMode3(List<ChannelData> channelDataList) {
        List<XYDataset> datasetList = new ArrayList<>();
        channelDataList.forEach(channelData -> {
            XYSeriesCollection collection = new XYSeriesCollection();
            XYSeries series = new XYSeries(channelData.getTotalDesc());
            final List<Double> values = channelData.getValues();
            for (int i = 0; i < values.size(); i++) {
                series.add(i, values.get(i));
            }
            collection.addSeries(series);
            datasetList.add(collection);
        });
        return datasetList;
    }
}
